<template>
    <div v-loading="buttonLoading">

        <CommonPage :state="state" :flowId="form.flowId" @submitForm="submitForm">
            <OrderAddInfo style="padding: 20px" ref="form" v-model="form" :state="state"></OrderAddInfo>
        </CommonPage>

        <!--        <OrderAddInfo style="padding: 20px" ref="form" v-model="form" :state="state"></OrderAddInfo>-->

        <!--        <div v-if="'write'==state" class="card-wrap flex justify-end card-padding footer-margin">-->
        <!--            <el-button @click="cancel">取消</el-button>-->
        <!--            <el-button :loading="buttonLoading" type="primary" plain @click="submitForm(1)">保存</el-button>-->
        <!--            <el-button :loading="buttonLoading" type="primary" @click="submitForm(2)">提交</el-button>-->
        <!--        </div>-->

    </div>
</template>

<script>


import {deptTreeSelect, listUser} from "@/api/system/user";
import {purchaseAdd, purchaseUpdate, purchaseDetail, queryParamList} from "@/api/purchaseManage/purchaseList";
import {v1 as uuid_v1} from 'uuid'
import {supplierList} from "@/api/memberManage/vendorList";
import OrderAddInfo from "@/views/purchaseManage/purchaseList/components/orderAddInfo.vue";

export default {
    name: "OrderPurchase",
    components: {OrderAddInfo},
    data() {
        return {
            buttonLoading: false,
            form: {},
            state: 'write',
            // form: {
            //     followerIds: null,
            //     purchaseInfoList: [{
            //         id: uuid_v1()
            //     }],
            //     orderCarInfoList:[]
            // },
            // supplierList: [],
            // dialogVisible: false,
            // deptOptions: null,
            // defaultProps: {
            //     children: "children",
            //     label: "label"
            // },
            // deptId: null,
            // deptLabel: null,
            // userList: [],
        };
    },
    watch: {
        // '$route': {
        //     handler(val) {
        //         console.log('this.$route', this.$route)
        //
        //         if (val && val.name == this.$options.name) {
        //             if (this.$route.query && this.$route.query.purchaseOrderId && !this.$route.query.tabClick) {
        //                 this.getOldData(this.$route.query.purchaseOrderId)
        //             } else if (!this.$route.query.tabClick) {
        //                 this.form = {
        //                     followerIds: null,
        //                     purchaseInfoList: [{
        //                         id: uuid_v1()
        //                     }],
        //                     orderCarInfoList: []
        //                 }
        //             }
        //             if (this.$route.query && 'detail' == this.$route.query.type) {
        //                 this.state = 'disabled'
        //             } else {
        //                 this.state = 'write'
        //             }
        //             const title = this.$route?.query?.pageType ?? '订单采购'
        //             const route = Object.assign({}, this.$route, {title: `${title}`})
        //             this.$store.dispatch('tagsView/updateVisitedView', route)
        //         }
        //     },
        //     immediate: true,
        //     deep: true
        // }
    },
    mounted() {
        // console.log('/clues/clues/{cluesId}==>', this.$route.query)
        if (this.$route.query && this.$route.query.purchaseOrderId) {
            this.getOldData(this.$route.query.purchaseOrderId)
        } else {
            this.form = {
                followerIds: null,
                purchaseInfoList: [{
                    item_id: uuid_v1(),
                    modelKey:0
                }],
                orderCarInfoList: []
            }
        }
        if (this.$route.query && 'detail' == this.$route.query.type) {
            this.state = 'disabled'
        } else {
            this.state = 'write'
        }
        const title = this.$route?.query?.pageType ?? '订单采购'
        const route = Object.assign({}, this.$route, {title: `${title}`})
        this.$store.dispatch('tagsView/updateVisitedView', route)
    },
    methods: {
        cancel() {
            this.$router.back();
            this.$store.dispatch('tagsView/delView', this.$route);
        },
        async submitForm(type) {
            let that = this

            this.form.saveFlag = type

            // const allNum = this.form.purchaseInfoList.reduce((pre, item)=>{
            //     return pre += Number(item.purchaseNum)
            // }, 0)
            //
            // const allordernum = this.form.orderCarInfoList.reduce((pre, item)=>{
            //     return pre += Number(item.carNum)
            // }, 0)


            if (0 == type) {
                let flag = await that.$refs.form.valid(1);
                if (flag) {
                    that.commit(0)
                }
            } else if (1 == type) {
                let result = await that.$refs.form.valid();

                if (result) {
                    that.commit()
                }
            }
        },

        commit(type) {
            // console.log('访问commit')
            let that = this
            this.buttonLoading = true;

            if (that.form.purchaseOrderId) {
                purchaseUpdate(that.form).then(res => {
                    this.buttonLoading = false;
                    if (0 == that.form.saveFlag) {
                        this.$modal.msgSuccess("保存成功");
                    } else if (1 == that.form.saveFlag) {
                        this.$modal.msgSuccess("提交成功");
                    }
                    if(0!==type){
                        this.$router.back();
                        this.$store.dispatch('tagsView/delView', this.$route);
                    }

                }).finally(e => {
                    this.buttonLoading = false;
                })
            } else {
                purchaseAdd(that.form).then(res => {
                    this.buttonLoading = false;
                    if (0 == that.form.saveFlag) {
                        this.$modal.msgSuccess("保存成功");
                    } else if (1 == that.form.saveFlag) {
                        this.$modal.msgSuccess("提交成功");
                    }
                    if(0!==type){
                        this.$router.back();
                        this.$store.dispatch('tagsView/delView', this.$route);
                    }
                }).finally(e => {
                    this.buttonLoading = false;
                })
            }
        },

        getOldData(purchaseOrderId) {
            let _this = this

            purchaseDetail(purchaseOrderId).then(res => {
                this.form = res.data

                // this.form.purchaseInfoList = this.form.orderCarInfoList.map(item=>{
                //     return {
                //         purchaseNum:item.carNum,
                //         ...item
                //     }
                // })
                //
                // for (const forElement of this.form.purchaseInfoList) {
                //     this.getSeatList(forElement)
                //
                //     forElement.id = uuid_v1()
                //
                //     forElement.modeldata = []
                //     forElement.modelKey = 0
                //     if (forElement.modelId && forElement.modelId != null) {
                //         //下标0为一级菜单value
                //         forElement.modeldata[0] = String(forElement.brandId);
                //         //下标1为二级菜单value
                //         forElement.modeldata[1] = String(forElement.seriesId);
                //         //下标1为三级菜单value
                //         forElement.modeldata[2] = String(forElement.modelId);
                //         forElement.modelKey++
                //         console.log('--', forElement.modeldata)
                //     }
                // }


            })
        },

    }
};
</script>


<style lang="scss" scoped>
::v-deep .el-form--label-top .el-form-item__label {
    padding: 0;
}

.el-form-item {
    margin-bottom: 15px;
    width: 70%;
}

.icon_add {
    margin-left: 10px;
    cursor: pointer;
}

</style>
